package com.factory.test.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.factory.test.R;
import com.factory.test.base.BaseTestActivity;
import com.factory.test.bean.SystemInfo;
import com.factory.test.databinding.ActivitySystemInfoBinding;
import com.factory.test.util.DateTimeUtil;


/**
 * 获取系统信息
 */
public class SystemInfoActivity extends BaseTestActivity {

    private ActivitySystemInfoBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_system_info);
        binding.setViewClick(new ViewClick());

        SystemInfo systemInfo = new SystemInfo();
        systemInfo.setEthernetMac(sdkService.getEthernetMac());
        systemInfo.setRam(sdkService.getRAMTotalMemorySize());
        systemInfo.setRom(sdkService.getROMTotalSize());
        systemInfo.setFirmwareVersion(sdkService.getRomVersion());
        //systemInfo.setFirmwareTime(sdkService.getFirmwareCompileTime());
        systemInfo.setCoreVersion(sdkService.getKernelVersion());
        systemInfo.setDeviceModel(Build.MODEL);
        systemInfo.setSystemVersion("Android " + Build.VERSION.RELEASE);
        systemInfo.setTimeZone(DateTimeUtil.getCurrentTimeZone());
        systemInfo.setResolution(getMetrics());
        systemInfo.setNetworkType(getNetworkState(context));
        systemInfo.setSn(getSn());

        binding.setInfo(systemInfo);
    }

    private String getSn(){
        if(Build.VERSION.SDK_INT >= 26){
            return Build.getSerial();
        }
        return Build.SERIAL;
    }



    private int clickCount = 0;
    private void tipClearMac(){
//        new AlertDialog.Builder(context, R.style.BDAlertDialog)
//                .setTitle("提示")
//                .setMessage("是否擦除MAC、SN等数据?")
//                .setPositiveButton("确定擦除", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        DeviceFeatures features = new DeviceFeatures();
//                        features.setMac("");
//                        features.setSn("");
//                        features.setKey("");
//                        features.setUuid("");
//                        SystemUtils.writeDeviceFeatures(features);
//                        SystemUtils.reboot(context);
//                    }
//                })
//                .setNeutralButton("取消", null)
//                .setCancelable(false)
//                .show().setCanceledOnTouchOutside(false);
    }

    private static String NETWORK_OTHER = "无网络";
    private static String NETWORK_WIFI = "WIFI";
    private static String NETWORK_2G = "2G";
    private static String NETWORK_3G = "3G";
    private static String NETWORK_4G = "4G";
    private static String NETWORK_5G = "5G";
    private static String NETWORK_ETHERNET = "以太网";

    public String getNetworkState(Context context) {
        ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); // 获取网络服务
        if (null == connManager) { // 为空则认为无网络
            return NETWORK_OTHER;
        }
        // 获取网络类型，如果为空，返回无网络
        NetworkInfo activeNetInfo = connManager.getActiveNetworkInfo();
        if (activeNetInfo == null || !activeNetInfo.isAvailable()) {
            return NETWORK_OTHER;
        }
        // 判断是否为WIFI
        NetworkInfo wifiInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (null != wifiInfo) {
            NetworkInfo.State state = wifiInfo.getState();
            if (null != state) {
                if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
                    return NETWORK_WIFI;
                }
            }
        }

        String ethernetIp = sdkService.getEthernetIp();        //如果以太网不为空
        if(isNotEmpoty(ethernetIp)){
            return NETWORK_ETHERNET;
        }

        return getNetworkToString(context);
    }

    @SuppressLint("MissingPermission")
    public static String getNetworkToString(Context context){
        // 若不是WIFI，则去判断是2G、3G、4G网
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (telephonyManager == null) return NETWORK_OTHER;
         int networkType = telephonyManager.getNetworkType();
        switch (networkType) {
            /*
             GPRS : 2G(2.5) General Packet Radia Service 114kbps
             EDGE : 2G(2.75G) Enhanced Data Rate for GSM Evolution 384kbps
             UMTS : 3G WCDMA 联通3G Universal Mobile Telecommunication System 完整的3G移动通信技术标准
             CDMA : 2G 电信 Code Division Multiple Access 码分多址
             EVDO_0 : 3G (EVDO 全程 CDMA2000 1xEV-DO) Evolution - Data Only (Data Optimized) 153.6kps - 2.4mbps 属于3G
             EVDO_A : 3G 1.8mbps - 3.1mbps 属于3G过渡，3.5G
             1xRTT : 2G CDMA2000 1xRTT (RTT - 无线电传输技术) 144kbps 2G的过渡,
             HSDPA : 3.5G 高速下行分组接入 3.5G WCDMA High Speed Downlink Packet Access 14.4mbps
             HSUPA : 3.5G High Speed Uplink Packet Access 高速上行链路分组接入 1.4 - 5.8 mbps
             HSPA : 3G (分HSDPA,HSUPA) High Speed Packet Access
             IDEN : 2G Integrated Dispatch Enhanced Networks 集成数字增强型网络 （属于2G，来自维基百科）
             EVDO_B : 3G EV-DO Rev.B 14.7Mbps 下行 3.5G
             LTE : 4G Long Term Evolution FDD-LTE 和 TDD-LTE , 3G过渡，升级版 LTE Advanced 才是4G
             EHRPD : 3G CDMA2000向LTE 4G的中间产物 Evolved High Rate Packet Data HRPD的升级
             HSPAP : 3G HSPAP 比 HSDPA 快些
             */
            // 2G网络
            case TelephonyManager.NETWORK_TYPE_GPRS:
            case TelephonyManager.NETWORK_TYPE_CDMA:
            case TelephonyManager.NETWORK_TYPE_EDGE:
            case TelephonyManager.NETWORK_TYPE_1xRTT:
            case TelephonyManager.NETWORK_TYPE_IDEN:
                return NETWORK_2G;
            // 3G网络
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
            case TelephonyManager.NETWORK_TYPE_UMTS:
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
            case TelephonyManager.NETWORK_TYPE_HSDPA:
            case TelephonyManager.NETWORK_TYPE_HSUPA:
            case TelephonyManager.NETWORK_TYPE_HSPA:
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
            case TelephonyManager.NETWORK_TYPE_EHRPD:
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                return NETWORK_3G;
            // 4G网络
            case TelephonyManager.NETWORK_TYPE_LTE:
            case 19:// 聚波载合 4G+
                return NETWORK_4G;
            // 5G
//            case TelephonyManager.NETWORK_TYPE_NR:// 需要 SdkVersion>=29
            case 20:// 当 SdkVersion<=28 直接写20
                return NETWORK_5G;
            default:
                return NETWORK_OTHER;
        }
    }

    public String getMetrics() {
        DisplayMetrics outMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getRealMetrics(outMetrics);
        int widthPixel = outMetrics.widthPixels;
        int heightPixel = outMetrics.heightPixels;
        return widthPixel + "x" + heightPixel;
    }


    public class ViewClick{
        public void onDate(){
            startActivity(new Intent(Settings.ACTION_DATE_SETTINGS));
        }

        public void onNetwork(){
            startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
        }
    }
}
